{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "afa3f719",
   "metadata": {},
   "source": [
    "# 实战任务\n",
    "\n",
    "* 将指定文件夹下的登记照批量换背景色\n",
    "\n",
    "\n",
    "* 对更换背景色后登记照进行图片压缩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1ad7a56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入工具包（removebg.py）\n",
    "import tinify\n",
    "import os\n",
    "# 函数 make_reg_photo：制作登记照， 函数 show_img：显示图片\n",
    "from removebg import make_reg_photo\n",
    "from img_utils import show_img, compress_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81e8e903",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 遍历目录\n",
    "root_dir = \"04_files\"\n",
    "src_dir = os.path.join(root_dir,\"原始图片\") # 路径为：04_files/原始图片\n",
    "out_dir = os.path.join(root_dir,\"输出图片\") # 路径为：04_files/输出图片\n",
    "\n",
    "# 使用前，请设置 API_KEY，需要自己去注册 https://www.remove.bg\n",
    "REMOVEBG_API_KEY = \"s5c9jxxxxxxxBUSay\"\n",
    "\n",
    "bg_colors = [\"red\",\"blue\",\"white\"]\n",
    "for name in os.listdir(src_dir):\n",
    "    # 原始图片的路径\n",
    "    rsc_photo_path = os.path.join(src_dir, name)\n",
    "    show_img(rsc_photo_path,\"原始图片\")\n",
    "    for color in  bg_colors:\n",
    "        # 处理背景颜色\n",
    "        reg_photo_file = make_reg_photo(REMOVEBG_API_KEY, rsc_photo_path, bg_color=color, out_dir=out_dir)\n",
    "        show_img(reg_photo_file, \"背景色:\"+color)  # 展示处理后的登记照片\n",
    "    # 删除去除背景文件（临时文件）\n",
    "    no_bg_file = rsc_photo_path + \"_no_bg.png\"   \n",
    "    if os.path.exists(no_bg_file):\n",
    "        os.remove(no_bg_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74fec573",
   "metadata": {},
   "outputs": [],
   "source": [
    "TINIFY_API_KEY = \"X3rjKxxxxxxxxlc7DkbmBgkM\"\n",
    "\n",
    "for name in os.listdir(out_dir):\n",
    "    photo_path = os.path.join(out_dir, name)\n",
    "#     print(photo_path)\n",
    "    show_img(photo_path,\"压缩前\")\n",
    "    \n",
    "    # 压缩后的图片路径\n",
    "    out_file = photo_path + \"_压缩.jpg\"\n",
    "    # 压缩文件\n",
    "    compress_image(TINIFY_API_KEY, photo_path, out_file)\n",
    "    \n",
    "    # 获取压缩后图片的大小(单位：字节)\n",
    "    src_file_size = os.path.getsize(photo_path)\n",
    "    out_file_size = os.path.getsize(out_file)\n",
    "    # 返回的图片大小是字节，这里除以1000后，单位则为k字节，并保留两位小数\n",
    "    print(\"原始文件大小：%sk\" % round(src_file_size/1000, 2))\n",
    "    print(\"压缩文件大小：%sk\" % round(out_file_size/1000, 2))\n",
    "    \n",
    "    # 删除压缩前的图片\n",
    "    # os.remove(photo_path)\n",
    "    \n",
    "    show_img(photo_path,\"压缩后\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91690917",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
